<?php
	
class Admin_CurrencyController extends TN_Controller_Action_Admin
{
 public function init()
    {
    	parent::init();
    }

 	public function indexAction()
 	{
    	$content = new Zend_Models_Currency();
 		$this->view->contents = $content->getAll();
 	}
 	
 	public function newcurrencyAction()
 	{
	    $form = $this->addCurrencyForm();
 		$this->view->form = $form;	
        if (!$this->getRequest()->isPost())
     	{
     	  	return;
     	}
     	else
    	{
    		if (!$form->isValid($_POST))
      		{
				$this->view->form = $form;
				return;
			}
			else
			{
				$values = $form->getValues();
				$content = new Zend_Models_Currency();
				$content = $content->createRow();
				$content->title = $values['common']['title'];
				$content->enabled = $values['common']['enabled'];
				$content->value = $values['common']['value'];
				$content->symbol = $values['common']['symbol'];
				$content->save();
				$this->_redirect("/admin/currency/index");
			}
		}
	 }
	 
	private function addCurrencyForm()
	 {
		$form = new Zend_Form();
	 	$form->setAction("/admin/currency/newcurrency")
	             ->setMethod('post');
    	$form->setAttrib("class", "jNice");         
        /**** Datos comunes ****/
    	$common = new Zend_Form_SubForm(); 
    	$enabled = $common->createElement('checkbox','enabled');
 	    $enabled->setLabel('Activo:');
 	    $common->addElement($enabled);	
 	    $title = $common->createElement("text",'title');
		$title->setAttrib("class", "text-long");
 	   	$title->addValidator('NotEmpty')->setLabel($this->view->t->_('*Titulo:'))->setRequired(true);
		$title->addErrorMessage($this->view->t->_('Este_campo_es_obligatorio'));
		$common->addElement($title);
		
		$value = $common->createElement("text", "value");
		$value->setAttrib("class", "text-long");
		$value->addValidator('Float')->setLabel('*Valor:')->setRequired(true);
		$value->addErrorMessage('Valor real invalido');
 	    $common->addElement($value);
 	    
 	    $symbol = $common->createElement("text",'symbol');
		$symbol->setAttrib("class", "text-long");
 	   	$symbol->addValidator('NotEmpty')->setLabel($this->view->t->_('*Simbolo:'))->setRequired(true);
		$symbol->addErrorMessage($this->view->t->_('Este_campo_es_obligatorio'));
		$common->addElement($symbol);
 	    
		$form->addSubForms(array("common" => $common));
	    /**** Fin datos comunes ***/
	    $form->addElement('submit', 'add', array('label' => 'Aceptar'));
		return $form;
	}
 	
 	public function editcurrencyAction()
 	{
 		$form = $this->editCurrencyForm();
 		$this->view->form = $form;	
 		if (!$this->getRequest()->isPost())
    	{
       		return;
     	}
    	 else
    	 {
    		if (!$form->isValid($_POST))
    	  	{
				$this->view->form = $form;
				return;
			}
			else
			{
				$values = $form->getValues();
				$currency = new Zend_Models_Currency();
				$currency = $currency->getCurrencyById($values['id_currency']);
				$currency->enabled = $values['common']['enabled'];
				$currency->title = $values['common']['title'];
			    $currency->value = $values['common']['value'];
				$currency->symbol = $values['common']['symbol'];
				$currency->save();
				$this->_redirect("/admin/currency/index");
			}
		}
 	}
 	
	private function editCurrencyForm()
	{
 		$form = new Zend_Form();
 		$form->setAction("/admin/currency/editcurrency")
       	      ->setMethod('post');
    	$form->setAttrib("class", "jNice");   
    	$id_currency = $this->_request->getParam('currency');
    
    	$currency = new Zend_Models_Currency();  
    	$currency = $currency->getCurrencyById($id_currency);         
         /**** Datos comunes ****/
    	$common = new Zend_Form_SubForm(); 
    	$enabled = $common->createElement('checkbox','enabled');
 	    $enabled->setLabel('Activo:');
 	    $enabled->setValue($currency->enabled);
 	    $common->addElement($enabled);
 	    $title = $common->createElement("text","title");
		$title->setAttrib("class", "text-long");
 	   	$title->addValidator('NotEmpty')->setLabel($this->view->t->_('*Titulo:'))->setRequired(true);
		$title->addErrorMessage($this->view->t->_('Este_campo_es_obligatorio'));
		$title->setValue($currency->title);
		$common->addElement($title);	
		
	    $value = $common->createElement("text", "value");
		$value->setAttrib("class", "text-long");
		$value->addValidator('Float')->setLabel('*Valor:')->setRequired(true);
		$value->addErrorMessage('Valor real invalido');
		$value->setValue($currency->value);
 	    $common->addElement($value);
 	    
 	    $symbol = $common->createElement("text",'symbol');
		$symbol->setAttrib("class", "text-long");
 	   	$symbol->addValidator('NotEmpty')->setLabel($this->view->t->_('*Simbolo:'))->setRequired(true);
		$symbol->addErrorMessage($this->view->t->_('Este_campo_es_obligatorio'));
		$symbol->setValue($currency->symbol);
		$common->addElement($symbol);
		
	    $form->addSubForms(array("common" => $common));
	    /**** Fin datos comunes ***/     
	 	$id = $form->createElement("hidden","id_currency");
		$id->setValue($currency->id);
    	$form->addElement($id);
     	$form->addElement('submit', 'add', array('label' => 'Aceptar'));
		return $form;
 }
 
 	public function deletecurrencyAction()
	{
 		$this->disableView();
 		$request = $this->_request->getParams();
    	$currency = $request['currency'];
   	 	$c = new Zend_Models_Currency();
   	 	$c = $c->getCurrencyById($currency);
    	$c->delete();
    	$this->_redirect("/admin/currency/index");
 	}
}    